#ifndef __ONLINE_H__
#define __ONLINE_H__
#include"util.hpp"
#include<mutex>
#include<websocketpp/config/asio_no_tls.hpp>//非ssl/tls加密
#include<websocketpp/server.hpp>
#include<unordered_map>

typedef websocketpp::server<websocketpp::config::asio> wsserver_t;
class online_manager
{
public:
    //websocket连接建立的时候才会加入游戏大厅&游戏房间在线用户管理
    void enter_game_hall(uint64_t uid,wsserver_t::connection_ptr &connection)
    {   
        std::unique_lock<std::mutex> lock(_mutex);
        _hall_user.insert(std::make_pair(uid,connection));
    }
    void enter_game_room(uint64_t uid,wsserver_t::connection_ptr &connection)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _room_user.insert(std::make_pair(uid,connection));
    }
    //websocket断开时候，会移除游戏大厅&游戏房间在线用户
    void exit_game_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _hall_user.erase(uid);
    }
    void  exit_game_room(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _room_user.erase(uid);

    }
    //判断指定用户是否在游戏大厅/游戏房间里
    bool is_in_game_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto it=_hall_user.find(uid);
        if(it==_hall_user.end()) return false;
        return true;

    }
    bool is_in_game_room(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto it=_room_user.find(uid);
        if(it==_room_user.end()) return false;
        return true;
    }
    //通过用户id在游戏大厅/游戏房间里获取对应的通信连接
    wsserver_t::connection_ptr get_conn_from_hall(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto it=_hall_user.find(uid);
        if(it==_hall_user.end()) return wsserver_t::connection_ptr();
        return it->second;
    }
    wsserver_t::connection_ptr get_conn_from_room(uint64_t uid)
    {
        std::unique_lock<std::mutex> lock(_mutex);
        auto it=_room_user.find(uid);
        if(it==_room_user.end()) return wsserver_t::connection_ptr();
        return it->second;
    }


private:
    //用于建立游戏大厅用户的用户ID与通信连接的关系
    std::unordered_map<uint64_t,wsserver_t::connection_ptr> _hall_user;
    //用于建立游戏房间用户的用户ID与通信连接的关系
    std::unordered_map<uint64_t,wsserver_t::connection_ptr> _room_user;
    std::mutex _mutex;
};

#endif 